/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections;

namespace BlindShark
{
	/// <summary>
	/// Description of ClockManager.
	/// </summary>
	public sealed class ClockManager
	{
		#region StaticElements
		private static ClockManager instance = new ClockManager();
		
		public static float GetTime()
		{
			return System.Environment.TickCount;
		}
		
		public static void AddTimeEvent(TimeEvent t)
		{
			instance.timeEventList.Add(t);
			instance.timeEventList.Sort();
		}
		
		public static void Tick()
		{
			instance.MeasureProgramSpeed();
			instance.ExecuteTimeEvents();
			instance.PurgeList();
			
			instance._iCumulatedTime += Convert.ToInt32(instance.timeBetweenFrames);
			instance._iFramesCounter++;
			if (instance._iCumulatedTime > 1000)
			{
				instance._iFPS = instance._iFramesCounter;
				instance._iFramesCounter = 0;
				instance._iCumulatedTime = 0;
			}
		}
		
		public static void Remove(TimeEvent t)
		{
			if (instance.timeEventList.Contains(t))
				instance.timeEventList.Remove(t);
		}
		
		public static int FPS
		{
			get
			{
//				if (instance.timeBetweenFrames!=0)
//					return Convert.ToInt32(1000 / instance.timeBetweenFrames);
//				else
//					return 0;
				return instance._iFPS;
			}
		}
		#endregion
		
		#region DynamicElements
		#region Variables
		private ArrayList timeEventList;
		private float startUpTime;
		private float timeBetweenFrames;
		private float timeOfLastTick;
		private int _iFramesCounter;
		private int _iFPS;
		private int _iCumulatedTime;
		#endregion
		private ClockManager()
		{
			timeEventList = new ArrayList();
			startUpTime = GetTime();
			timeOfLastTick = GetTime();
			_iFramesCounter = 0;
			_iFPS = 0;
			_iCumulatedTime = 0;
		}
		
		private void ExecuteTimeEvents()
		{
//			foreach(TimeEvent t in timeEventList)
//			{
//				if (t.IsPaused)
//					continue;
//				
//				t.SpendTime(timeBetweenFrames);
//				
//				if (t.TimeLeft<=0)
//				{
//					t.ExecCall();
//					if (!t.IsImmortal)
//						t.DecreaseLife();
//					t.Reset();
//				}
//			}
			
			for (int i=0; i<timeEventList.Count; i++)
			{
				TimeEvent t = (TimeEvent) timeEventList[i];
				if (t.IsPaused)
					continue;
				
				t.SpendTime(timeBetweenFrames);
				
				if (t.TimeLeft<=0)
				{
					t.ExecCall();
					if (!t.IsImmortal)
						t.DecreaseLife();
					t.Reset();
				}
			}
		}
		
		private void PurgeList()
		{
			int currentListSize = timeEventList.Count;
			TimeEvent t;
			for (int i=0; i<currentListSize; i++)
			{
				t = (TimeEvent) timeEventList[i];
				if (t.IsDead)
				{
					timeEventList.Remove(t);
					currentListSize--;
				}
			}
		}
		
		private void MeasureProgramSpeed()
		{
			timeBetweenFrames = GetTime() - timeOfLastTick;
			timeOfLastTick = GetTime();
		}
		#endregion
	}
}
